package com.example.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.news.mapper.ArticleMapper;
import com.example.news.pojo.entity.Article;
import com.example.news.pojo.form.ArticleForm;
import com.example.news.pojo.form.BasePageQuery;
import com.example.news.pojo.vo.ArticleCommentVo;
import com.example.news.pojo.vo.ArticleDetailVo;
import com.example.news.pojo.vo.ArticleVo;
import com.example.news.service.ArticleService;
import com.example.news.service.CommentService;
import com.example.news.utils.JwtHelper;
import com.example.news.utils.result.PageResult;
import com.example.news.utils.result.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>
        implements ArticleService {

    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    private CommentService commentService;
    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result<String> createArticle(ArticleForm request,String token) {
        Long id = jwtHelper.getUserId(token);
        Article article = new Article();
        BeanUtils.copyProperties(request, article);
        article.setUserId(id.intValue());

        articleMapper.insert(article);

        return Result.ok("新闻保存成功");
    }

    @Override
    public Result<String> deleteArticleById(Integer articleId) {
        articleMapper.deleteById(articleId);
        return Result.ok("新闻删除成功");
    }

    @Override
    public Result<String> modifyArticle(ArticleForm request) {
        Article ariticle = new Article();
        ariticle.setId(request.getId());
        ariticle.setTitle(request.getTitle());
        ariticle.setCover(request.getCover());
        ariticle.setContent(request.getContent());
        ariticle.setType(request.getType());

        articleMapper.updateById(ariticle);
        return Result.ok("新闻修改成功");
    }

    @Override
    public Result<String> publishArticle(ArticleForm request) {
        Article ariticle = new Article();

        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        Date date = new Date(timestamp.getTime());
        ariticle.setPublishTime(date);


        UpdateWrapper<Article> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", request.getId()); // 设置更新条件
        ariticle.setTitle(request.getTitle()); // 设置要更新的字段
        ariticle.setContent(request.getContent());
        ariticle.setType(request.getType());
        ariticle.setStatus(1);

        articleMapper.update(ariticle, updateWrapper);
        return Result.ok("新闻发布成功");
    }

    @Override
    public Result<String> approveArticle(Integer articleId) {
        articleMapper.updateStatus(articleId, 2);
        return Result.ok("该新闻已审核成功");
    }

    @Override
    public Result<String> rejectArticle(Integer articleId) {
        articleMapper.updateStatus(articleId, 3);
        return Result.ok("该新闻已审核失败");
    }

    @Override
    public Result<ArticleDetailVo> getArticleById(Integer articleId) {
        ArticleDetailVo articleDetailVo = articleMapper.getArticleById(articleId);
        if (articleDetailVo != null) {
            Result<ArticleCommentVo> data = commentService.getArticleCommentByArticleId(articleId);
            if(data.getData() == null){
                ArticleCommentVo none = new ArticleCommentVo();
                none.setCommentNum(0);
                articleDetailVo.setComment(none);
            }else{
                articleDetailVo.setComment(data.getData());
            }
            return Result.ok(articleDetailVo);
        }
        return Result.ok(null, "文章不存在！");
    }

    @Override
    public PageResult<ArticleVo> userGetArticles(BasePageQuery basePageQuery,Integer typeId) {
        Page<Article> page = new Page<>(basePageQuery.getPageNum(), basePageQuery.getPageSize());
        IPage<ArticleVo> iPage = articleMapper.userGetArticles(page,typeId);
        return PageResult.success(iPage);
    }

    @Override
    public PageResult<ArticleVo> adminGetArticles(BasePageQuery basePageQuery) {
        Page<Article> page = new Page<>(basePageQuery.getPageNum(), basePageQuery.getPageSize());
        IPage<ArticleVo> iPage = articleMapper.adminGetArticles(page);
        return PageResult.success(iPage);
    }
}
